package com.example.demo.controller;

import com.example.demo.dto.PieDto;
import com.example.demo.entity.*;
import com.example.demo.service.HospitalService;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hospital")
public class HospitalController {
    @Autowired
    private HospitalService hospitalService;
    @RequestMapping("/show_hospital_page")
    public String show_hospital_page(Model model){
        int hospital_num=0;
        List<Hospital> hospitals=hospitalService.findHospital();
        List<HospitalTypeInfo> hospitalTypeInfos=hospitalService.findHospitalTypeInfo();
        List<HospitalAreaInfo> hospitalAreaInfos=hospitalService.findHospitalAreaInfo();
        List<HospitalPointRank> hospitalPointRanks=hospitalService.findHospitalPointRank();
        List<HospitalPopularityRank> hospitalPopularityRanks=hospitalService.findHospitalPopularityRank();
        double hospital_average_money = 0;
        double money=0;
        double hospital_average_point=0;
        double points=0;
        int hospital_type_num=0;
        int hospital_area_num=0;
        String hospital_hot_type=null;
        hospital_num=hospitals.size();
        for (int i=0;i<hospital_num;i++){
            Hospital hospital =hospitals.get(i);
            money=money+hospital.getAverage();
            points=points+hospital.getPoint();
        }
        hospital_average_money=money/hospital_num;
        BigDecimal a = new BigDecimal(hospital_average_money);
        hospital_average_money=a.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        hospital_average_point=points/hospital_num;
        BigDecimal b =new BigDecimal(hospital_average_point);
        hospital_average_point=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        hospital_type_num=hospitalTypeInfos.size();
        hospital_area_num=hospitalAreaInfos.size();
        HospitalTypeInfo hospitalTypeInfoMax=hospitalTypeInfos.get(0);
        for (int i=0;i<hospital_type_num;i++){
            HospitalTypeInfo hospitalTypeInfo=hospitalTypeInfos.get(i);
            if (hospitalTypeInfo.getNum()>hospitalTypeInfoMax.getNum()){
                hospitalTypeInfoMax=hospitalTypeInfo;
            }
        }
        hospital_hot_type=hospitalTypeInfoMax.getType();
        model.addAttribute("hospital_num",hospital_num);
        model.addAttribute("hospital_average_money",hospital_average_money);
        model.addAttribute("hospital_average_point",hospital_average_point);
        model.addAttribute("hospital_type_num",hospital_type_num);
        model.addAttribute("hospital_area_num",hospital_area_num);
        model.addAttribute("hospital_hot_type",hospital_hot_type);
        model.addAttribute("hospitalPointRanks",hospitalPointRanks.subList(0,20));
        model.addAttribute("hospitalPopularityRanks",hospitalPopularityRanks.subList(0,20));
        return "hospital";
    }

    @RequestMapping("/hospitalInfo")
    public ModelAndView hospitalInfo(ModelAndView modelAndView,@RequestParam(defaultValue="1")Integer
            pageNum,@RequestParam(defaultValue="15")Integer pageSize){
        PageInfo<Hospital> hospitalPageInfo=hospitalService.selectHospitalPage(pageNum,pageSize);
        modelAndView.addObject("hospitalPageInfo",hospitalPageInfo);
        modelAndView.setViewName("hospitalInfo");
        return modelAndView;
    }
    @RequestMapping("/hospital_echarts_1")
    @ResponseBody
    public List<PieDto> hospital_echarts_1(){
        List<PieDto> pieDtos=new ArrayList<>();
        List<HospitalTypeInfo> hospitalTypeInfos =hospitalService.findHospitalTypeInfo();
        for (int i=0;i<hospitalTypeInfos.size();i++){
            HospitalTypeInfo hospitalTypeInfodb=hospitalTypeInfos.get(i);
            PieDto pieDtodb=new PieDto();
            pieDtodb.setName(hospitalTypeInfodb.getType());
            pieDtodb.setValue(hospitalTypeInfodb.getNum());
            pieDtos.add(pieDtodb);
        }
        return pieDtos;
    }
    @RequestMapping("/hospital_echarts_2_v")
    @ResponseBody
    public List<List<Double>> hospital_echarts_2_v(){
        List<List<Double>> vlists=new ArrayList<>();
        List<Double> list1=new ArrayList<>();
        List<Double> list2=new ArrayList<>();
        List<HospitalTypeInfo> hospitalTypeInfos =hospitalService.findHospitalTypeInfo();
        for (int i =0;i<hospitalTypeInfos.size();i++){
            HospitalTypeInfo hospitalTypeInfo =hospitalTypeInfos.get(i);
            list1.add((double)hospitalTypeInfo.getNum());
            list2.add(hospitalTypeInfo.getAverage());
        }
        vlists.add(list1);
        vlists.add(list2);
        return vlists;
    }
    @RequestMapping("/hospital_echarts_2_x")
    @ResponseBody
    public List<String> hospital_echarts_2_x(){
        List<String> xlist=new ArrayList<>();
        List<HospitalTypeInfo> hospitalTypeInfos=hospitalService.findHospitalTypeInfo();
        for (int i=0;i<hospitalTypeInfos.size();i++){
            HospitalTypeInfo hospitalTypeInfo=hospitalTypeInfos.get(i);
            xlist.add(hospitalTypeInfo.getType());
        }
        return xlist;
    }
    @RequestMapping("/hospital_echarts_3")
    @ResponseBody
    public List<PieDto> hospital_echarts_3(){
        List<PieDto> pieDtos=new ArrayList<>();
        List<HospitalAreaInfo> hospitalAreaInfos=hospitalService.findHospitalAreaInfo();
        for (int i =0;i<hospitalAreaInfos.size();i++){
            HospitalAreaInfo hospitalAreaInfo=hospitalAreaInfos.get(i);
            PieDto pieDtodb=new PieDto();
            pieDtodb.setName(hospitalAreaInfo.getArea());
            pieDtodb.setValue(hospitalAreaInfo.getNum());
            pieDtos.add(pieDtodb);
        }
        return pieDtos;
    }
    @RequestMapping("/insertHospital")
    public String insertHospital(Model model){
        List<HospitalAreaInfo> hospitalAreaInfos=hospitalService.findHospitalAreaInfo();
        List<HospitalTypeInfo> hospitalTypeInfos=hospitalService.findHospitalTypeInfo();
        model.addAttribute("area",hospitalAreaInfos);
        model.addAttribute("type",hospitalTypeInfos);
        return "insertHospital";
    }
    @RequestMapping("/doinsertHospital")
    @ResponseBody
    public Map<String,Object> doinsertHospital(Hospital hospital){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        Hospital hospitaldb=hospitalService.findHospitalByName(hospital.getName());
        if (hospitaldb!=null){
            resultMap.put("result","这家医院已经存在啦");
        }else {
            hospitalService.insertHospital(hospital);
            resultMap.put("result","SUCCESS");
        }
        return resultMap;
    }
    @RequestMapping("/updateHospital")
    public String updateHospital(Model model,int id){
        Hospital hospital=hospitalService.findHospitalById(id);
        List<HospitalAreaInfo> hospitalAreaInfos=hospitalService.findHospitalAreaInfo();
        List<HospitalTypeInfo> hospitalTypeInfos=hospitalService.findHospitalTypeInfo();
        model.addAttribute("area",hospitalAreaInfos);
        model.addAttribute("type",hospitalTypeInfos);
        model.addAttribute("hospital",hospital);
        return "updateHospital";
    }
    @RequestMapping("/doupdateHospital")
    @ResponseBody
    public Map<String,Object> doupdateHospital(Hospital hospital){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        Hospital hospitaldb=hospitalService.findHospitalByName(hospital.getName());
        if (hospitaldb!=null&&hospitaldb.getId()!=hospital.getId()){
            resultMap.put("result","这家医院已经存在啦");
        }
        else {
            Hospital hospital1=hospitalService.findHospitalById(hospital.getId());
            hospitalService.updateHospitalEX(hospital1);
            hospitalService.updateHospital(hospital);
            resultMap.put("result","SUCCESS");
        }
        return resultMap;
    }
    @RequestMapping("/deleteHospital")
    @ResponseBody
    public Map<String,Object> deleteHospital(int id){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        hospitalService.deleteHospital(id);
        resultMap.put("result","SUCCESS");
        return resultMap;
    }


}
